/**
 * Copyright © 2016-2025 The Thingsboard Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.terracloud.server.dao.sql.device;

import com.terracloud.server.common.data.device.DeviceAccessControlMode;
import com.terracloud.server.common.data.device.DeviceAccessControlRuleType;
import com.terracloud.server.dao.model.sql.DeviceAccessControlRuleEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.UUID;

public interface DeviceAccessControlRuleRepository extends JpaRepository<DeviceAccessControlRuleEntity, UUID> {

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.tenantId = :tenantId AND r.value = :value")
    DeviceAccessControlRuleEntity findByTenantIdAndValue(@Param("tenantId") UUID tenantId, @Param("value") String value);
    
    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.value = :value AND r.type = :type")
    List<DeviceAccessControlRuleEntity> findByValueAndType(@Param("value") String value, @Param("type") DeviceAccessControlRuleType type);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE LOWER(r.value) LIKE LOWER(CONCAT('%', :textSearch, '%'))")
    Page<DeviceAccessControlRuleEntity> findByTextSearch(@Param("textSearch") String textSearch, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.type = :type AND LOWER(r.value) LIKE LOWER(CONCAT('%', :textSearch, '%'))")
    Page<DeviceAccessControlRuleEntity> findByTypeAndTextSearch(@Param("type") DeviceAccessControlRuleType type, @Param("textSearch") String textSearch, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.mode = :mode AND LOWER(r.value) LIKE LOWER(CONCAT('%', :textSearch, '%'))")
    Page<DeviceAccessControlRuleEntity> findByModeAndTextSearch(@Param("mode") DeviceAccessControlMode mode, @Param("textSearch") String textSearch, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.type = :type AND r.mode = :mode AND LOWER(r.value) LIKE LOWER(CONCAT('%', :textSearch, '%'))")
    Page<DeviceAccessControlRuleEntity> findByTypeAndModeAndTextSearch(@Param("type") DeviceAccessControlRuleType type, @Param("mode") DeviceAccessControlMode mode, @Param("textSearch") String textSearch, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.tenantId = :tenantId AND LOWER(r.value) LIKE LOWER(CONCAT('%', :textSearch, '%'))")
    Page<DeviceAccessControlRuleEntity> findByTenantIdAndTextSearch(@Param("tenantId") UUID tenantId, @Param("textSearch") String textSearch, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.tenantId = :tenantId AND r.type = :type AND LOWER(r.value) LIKE LOWER(CONCAT('%', :textSearch, '%'))")
    Page<DeviceAccessControlRuleEntity> findByTenantIdAndTypeAndTextSearch(@Param("tenantId") UUID tenantId, @Param("type") DeviceAccessControlRuleType type, @Param("textSearch") String textSearch, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.tenantId = :tenantId AND r.mode = :mode AND LOWER(r.value) LIKE LOWER(CONCAT('%', :textSearch, '%'))")
    Page<DeviceAccessControlRuleEntity> findByTenantIdAndModeAndTextSearch(@Param("tenantId") UUID tenantId, @Param("mode") DeviceAccessControlMode mode, @Param("textSearch") String textSearch, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.tenantId = :tenantId AND r.type = :type AND r.mode = :mode AND LOWER(r.value) LIKE LOWER(CONCAT('%', :textSearch, '%'))")
    Page<DeviceAccessControlRuleEntity> findByTenantIdAndTypeAndModeAndTextSearch(@Param("tenantId") UUID tenantId, @Param("type") DeviceAccessControlRuleType type, @Param("mode") DeviceAccessControlMode mode, @Param("textSearch") String textSearch, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r")
    Page<DeviceAccessControlRuleEntity> findAll(Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.type = :type")
    Page<DeviceAccessControlRuleEntity> findByType(@Param("type") DeviceAccessControlRuleType type, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.mode = :mode")
    Page<DeviceAccessControlRuleEntity> findByMode(@Param("mode") DeviceAccessControlMode mode, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.type = :type AND r.mode = :mode")
    Page<DeviceAccessControlRuleEntity> findByTypeAndMode(@Param("type") DeviceAccessControlRuleType type, @Param("mode") DeviceAccessControlMode mode, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.tenantId = :tenantId")
    Page<DeviceAccessControlRuleEntity> findByTenantId(@Param("tenantId") UUID tenantId, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.tenantId = :tenantId AND r.type = :type")
    Page<DeviceAccessControlRuleEntity> findByTenantIdAndType(@Param("tenantId") UUID tenantId, @Param("type") DeviceAccessControlRuleType type, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.tenantId = :tenantId AND r.mode = :mode")
    Page<DeviceAccessControlRuleEntity> findByTenantIdAndMode(@Param("tenantId") UUID tenantId, @Param("mode") DeviceAccessControlMode mode, Pageable pageable);

    @Query("SELECT r FROM DeviceAccessControlRuleEntity r WHERE r.tenantId = :tenantId AND r.type = :type AND r.mode = :mode")
    Page<DeviceAccessControlRuleEntity> findByTenantIdAndTypeAndMode(@Param("tenantId") UUID tenantId, @Param("type") DeviceAccessControlRuleType type, @Param("mode") DeviceAccessControlMode mode, Pageable pageable);
}